package ym.android.async.http;

import ym.transfer.http.HttpException;
import ym.transfer.http.HttpRequest;
import ym.transfer.http.HttpResponse;
import ym.transfer.http.HttpTransfer;
import android.os.AsyncTask;

public abstract class HttpAsync extends AsyncTask<HttpRequest, Integer, HttpResponse> {
    private HttpException exception;
    
    @Override  
    protected HttpResponse doInBackground(HttpRequest... params) {
        try {
            return HttpTransfer.execute(params[0]);
        } catch (HttpException e) {
            exception = e;
            return null;
        }
    }
    
    @Override  
    protected void onPostExecute(HttpResponse response) {
        if(response != null) {
            onCompelte(response);
        } else {
            onError(exception);
        }
    }

    protected abstract void onCompelte(HttpResponse response);
    protected abstract void onError(HttpException exception);
}